package com.meetup.caug.gitrdone;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.meetup.caug.gitrdone.model.Task;

public class MainActivity extends Activity {

    MainApplication ma;

    List<Task> taskList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ma = (MainApplication) getApplication();
        taskList = ma.getTasks();

        Log.v("ez", "taskList size before shared prefs " + taskList.size());

        // If the app is still in memory don't get tasks from shared preferences
        if (taskList.isEmpty()) {
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(this);
            int numberOfTasks = sp.getInt("numTasks", 0);
            for (int i = 1; i <= numberOfTasks; i++) {
                taskList.add(new Task(sp.getString("task" + i, "")));
            }
        }

        Log.v("ez", "taskList size after shared prefs " + taskList.size());

        Button addTask = (Button) findViewById(R.id.addTaskBTN);
        Button listTasks = (Button) findViewById(R.id.listTaskBTN);

        listTasks.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, TaskList.class);
                startActivity(intent);
            }
        });

        addTask.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, AddTask.class);
                startActivity(intent);
            }
        });
    }
}
